using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BackgroundProcessor : MonoBehaviour {

    public GameObject[] _prefab;
	protected int _curIndex;
	protected int _instanceCount;
    protected Queue<GameObject> _backgroundQueue;
	
	void Awake()
	{
		_backgroundQueue = new Queue<GameObject>();	
		_curIndex = 0;
	}
	
    void LateUpdate()
    {
		if(_backgroundQueue == null)
			return ;
		
		if(_prefab == null || _prefab.Length == 0)
			return ;
		
		if(_backgroundQueue.Count < 3)
		{
			GameObject bgPrefab = _prefab[_curIndex];
			
			float calcY = Screen.currentResolution.height * _instanceCount;
			
			_backgroundQueue.Enqueue( Util.AttachGameObject(bgPrefab , gameObject ,  new Vector3(0, calcY , 0)));
			_instanceCount++;
		}
		else if(Camera.mainCamera.transform.localPosition.y > (Screen.currentResolution.height + 20 + _backgroundQueue.Peek().transform.localPosition.y) )
		{
			Util.SafeDestroy(_backgroundQueue.Peek());
			_backgroundQueue.Dequeue();
		}
    }


}
